package com.workstudy.wstudy.service.impl;

import java.util.List;

import com.workstudy.common.core.domain.entity.SysUser;
import com.workstudy.common.utils.DateUtils;
import com.workstudy.common.utils.ShiroUtils;
import com.workstudy.system.service.ISysUserService;
import org.apache.shiro.crypto.hash.Md5Hash;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.workstudy.wstudy.mapper.WstTeacherMapper;
import com.workstudy.wstudy.domain.WstTeacher;
import com.workstudy.wstudy.service.IWstTeacherService;
import com.workstudy.common.core.text.Convert;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

/**
 * 教师管理Service业务层处理
 * 
 * @author sun
 * @date 2021-04-04
 */
@Service
public class WstTeacherServiceImpl implements IWstTeacherService 
{
    @Autowired
    private WstTeacherMapper wstTeacherMapper;

    @Autowired
    private ISysUserService userService;

    /**
     * 查询教师管理
     * 
     * @param teacherId 教师管理ID
     * @return 教师管理
     */
    @Override
    public WstTeacher selectWstTeacherById(Long teacherId)
    {
        return wstTeacherMapper.selectWstTeacherById(teacherId);
    }

    /**
     * 查询教师管理列表
     * 
     * @param wstTeacher 教师管理
     * @return 教师管理
     */
    @Override
    public List<WstTeacher> selectWstTeacherList(WstTeacher wstTeacher)
    {
        return wstTeacherMapper.selectWstTeacherList(wstTeacher);
    }

    /**
     * 新增教师管理
     * 
     * @param wstTeacher 教师管理
     * @return 结果
     */
    @Override
    @Transactional(isolation= Isolation.DEFAULT,propagation= Propagation.REQUIRED,rollbackFor = Exception.class)
    public int insertWstTeacher(WstTeacher wstTeacher)
    {
        SysUser user = wstTeacher;
        user.setSalt(ShiroUtils.randomSalt());
        user.setPassword(new Md5Hash(user.getLoginName() + user.getPassword() + user.getSalt()).toHex());
        user.setCreateBy(ShiroUtils.getLoginName());
        userService.insertUser(user);
        wstTeacher.setTeacherId(user.getUserId());
        wstTeacher.setCreateTime(DateUtils.getNowDate());
        wstTeacher.setCreateTime(DateUtils.getNowDate());
        return wstTeacherMapper.insertWstTeacher(wstTeacher);
    }

    /**
     * 修改教师管理
     * 
     * @param wstTeacher 教师管理
     * @return 结果
     */
    @Override
    @Transactional(isolation= Isolation.DEFAULT,propagation= Propagation.REQUIRED,rollbackFor = Exception.class)
    public int updateWstTeacher(WstTeacher wstTeacher)
    {
        SysUser user = wstTeacher;
        userService.updateUser(user);
        wstTeacher.setUpdateTime(DateUtils.getNowDate());
        return wstTeacherMapper.updateWstTeacher(wstTeacher);
    }

    /**
     * 删除教师管理对象
     * 
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    @Transactional(isolation= Isolation.DEFAULT,propagation= Propagation.REQUIRED,rollbackFor = Exception.class)
    public int deleteWstTeacherByIds(String ids)
    {
        userService.deleteUserByIds(ids);
        return wstTeacherMapper.deleteWstTeacherByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除教师管理信息
     * 
     * @param teacherId 教师管理ID
     * @return 结果
     */
    @Override
    @Transactional(isolation= Isolation.DEFAULT,propagation= Propagation.REQUIRED,rollbackFor = Exception.class)
    public int deleteWstTeacherById(Long teacherId)
    {
        userService.deleteUserById(teacherId);
        return wstTeacherMapper.deleteWstTeacherById(teacherId);
    }
}
